﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietThuongLMS.ViewModels;
using VietThuongLMS.Services;
using VietThuongLMS.Utils;
using DCS.ComLib.Config;
using VietThuongLMS.Areas.Admin.Models;
//Author : Kha
namespace VietThuongLMS.Areas.Admin.Controllers
{
	public class SalaryController : Controller
    {
        [Authorize(Roles = "Admin")]
		public ActionResult Edit(int id)
		{
            var llh_gv = new LoaiLopHoc_GiaoVienService().GetLoaiLopHocGiaoVienByID(id);
            LoaiLopHoc_GiaoVienModel llh_gvm = new LoaiLopHoc_GiaoVienModel(llh_gv);
            var list = LoaiLopHocService.GetAll();
            ViewData["loailophoc"] = new SelectList(list, "ID", "TenLoaiLopHoc", llh_gv.MaLoaiLopHoc);
            return PartialView("pvSuaLoaiLopHoc_GiaoVien", llh_gvm);
		}

        [Authorize(Roles = "Admin")]
		public ActionResult Update(LoaiLopHoc_GiaoVienModel llh_gvm)
		{
            LoaiLopHoc_GiaoVien llh_gv = llh_gvm.ChuyenDoiModel();
            bool rs = new LoaiLopHoc_GiaoVienService().UpdateLoaiLopHoc_GiaoVien(llh_gv);
            return RedirectToAction("DanhSachLuongGiaoVien", "Salary", new {mgv=llh_gv.MaGiaoVien ,area = "admin" });
		}

        [Authorize(Roles = "Admin")]
		public ActionResult DoDelete(int id, int mgv)
		{
            bool rs = new LoaiLopHoc_GiaoVienService().DeleteOneLoaiLopHoc_GiaoVien(id);
            return RedirectToAction("DanhSachLuongGiaoVien", "Salary", new { mgv = mgv, area = "admin" });
		}

        [Authorize(Roles = "Admin")]
		public ActionResult Create(int mgv)
		{
			ViewData["loailophoc"] = new SelectList(LoaiLopHocService.GetAll(), "ID", "TenLoaiLopHoc");
            ViewData["magiaovien"] = mgv;
            return PartialView("pvTaoLoaiLopHoc_GiaoVien");
		}

        [Authorize(Roles = "Admin")]
        public ActionResult DoCreate(LoaiLopHoc_GiaoVienModel llh_gvm)
		{
            LoaiLopHoc_GiaoVien llh_gv = llh_gvm.ChuyenDoiModel();
            bool rs = new LoaiLopHoc_GiaoVienService().Insert(llh_gv);
            return RedirectToAction("DanhSachLuongGiaoVien", "Salary", new { mgv = llh_gv.MaGiaoVien, area = "admin" });
		}

        [Authorize(Roles = "Admin")]
		public ActionResult Calculate()
		{
			return View("TinhLuongChoGiaoVien");
		}

        [Authorize(Roles = "Admin")]
		public ActionResult Search(string kw)
		{         
            int page = 1;
            if (Request["p"] != null)
            {
                page = int.Parse(Request["p"].ToString());
            }

            var acvm = AccountService.GetTaiKhoanGiaoVienWithPagination(kw, 10, page);
            ////phân trang
            //List<int> args = new List<int>();
            //args.Add(op);
            ViewData["pageLinks"] = Utils.GeneralHTMLString.HTMLForJavaScriptPagination(acvm.TotalMoRong, 10, page, "searchingRequest");
            return PartialView("pvKetQuaTimKiemGiaoVien", acvm);
		}

        [Authorize(Roles = "Admin,NhanVien")]
		public ActionResult TimPhanCong(int id, int m, int y)
		{
            int page = 1;
            if (Request["p"] != null)
            {
                page = int.Parse(Request["p"].ToString());
            }
            var pcvm= new PhanCongViewModel();
            if (HttpContext.User.IsInRole("NhanVien")==true)
            {
                int cn = -1;
                string mtk = HttpContext.User.Identity.Name.ToString();
                cn = new NhanVienService().GetChiNhanhNhanVien(mtk);
                pcvm = PhanCongService.PhanCongTheoThangChiNhanh(id, m, y, cn, 10, page);
            }
            if (HttpContext.User.IsInRole("Admin") == true)
            {
                pcvm = PhanCongService.PhanCongTheoThang(id, m, y, 10, page);
            }
            if (pcvm.ListMoRong.Count == 0)
                return this.Content("Giáo viên này không dạy lớp nào trong tháng " + m.ToString() + "/" + y.ToString());
            //phân trang
            List<int> args = new List<int>();
            args.Add(id);
            args.Add(m);
            args.Add(y);
            ViewData["pageLinks"] = Utils.GeneralHTMLString.HTMLForJavaScriptPagination(pcvm.TotalMoRong, 10, page, "searchingClassTaskOnPage", args);
            ViewData["month"] = m;
            ViewData["year"] = y;
            ViewData["id"] = id;
            return PartialView("pvKetQuaTimPhanCong", pcvm);
		}

        [Authorize(Roles = "Admin")]
        public ActionResult DanhSachLuongGiaoVien(int mgv)
        {
            int page = 1;
            if (Request["p"] != null)
            {
                page = int.Parse(Request["p"].ToString());
            }
            LoaiLopHoc_GiaoVienViewModel llh_gvvm = new LoaiLopHoc_GiaoVienService().GetBangLuongByMaGiaoVienWithPagination(mgv, 10, page);
            //phân trang
            List<int> args = new List<int>();
            args.Add(mgv);
            ViewData["pageLinks"] = Utils.GeneralHTMLString.HTMLForJavaScriptPagination(llh_gvvm.TotalMoRong, 10, page, "searchingLoaiLopHoc_GiaoVien", args);
            ViewData["mgv"] = mgv;
            //end phân trang  
            return PartialView("pvDanhSachLoaiLopHoc_GiaoVien", llh_gvvm);
        }
	}
}
